{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c5d2d700",
   "metadata": {},
   "source": [
    "####  函数的优势总结\n",
    "\n",
    "1. 内置函数之前学过的 input() 、print()、range()、len() 函数等等，这些都是 Python 的内置函数，可以按需求直接使用。  \n",
    "2. 自定义函数，可以将一段可重复使用的代码定义成函数，从而达到一次编写、多次调用的目的即 降低代码的冗余度。  \n",
    "3. 自定义函数可以使不是很懂代码的人很快就能上手，只需输入所需参数就可以得到结果。  \n",
    "4. 当函数代码出现错误时，只需要修改一处就可以\n",
    "5. 函数代表一种封装，通过封装可以隐藏操作细节，函数可以表示操作的性能，而读者不需要了解操作的具体运行细节。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7193a05e",
   "metadata": {},
   "source": [
    "#### 课本代码实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b77ccc13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:my is a cutrpol\n",
      "o\n",
      "i\n",
      "u\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 实践一：\n",
    "def search4vowels():\n",
    "    '''display any vowels foun in an asked-for word'''    # 通过注释我们就能够知道这个函数的参数是str（字符串）\n",
    "    vowels = set('aeiou')\n",
    "    word = input(\"Provide a word to search for vowels:\")\n",
    "    found = vowels.intersection(set(word))\n",
    "    for vowel in found:\n",
    "        print(vowel)\n",
    "search4vowels()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b9f80654",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'u'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实践二：——默认参数\n",
    "def search4letters(phrase:str,letters:str='aeiou') ->set:  # 为letters赋了一个默认值\"aeiou\",如果调用代码块没有提供替代值，就会使用这个默认值\n",
    "    \"\"\"Return  any vowels found in a supplied phrase.\"\"\"\n",
    "    return set(letters).intersection(set(phrase))\n",
    "search4letters('life,the universe,and everything')         # 没有给出第二个参数，所以使用了默认的参数集合\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd5ee79e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'u'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4letters('life,the universe,and everything','aeiou')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "874d21f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x', 'z'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ——关键字参数 \n",
    "def search4letters(phrase:str,letters:str='aeiou') ->set:\n",
    "    \"\"\"Return  any vowels found in a supplied phrase.\"\"\"\n",
    "    return set(letters).intersection(set(phrase))\n",
    "search4letters(letters='xyz',phrase='galaxz')  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ad4e250",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实践三：—— return 返回一个值  \n",
    "def search_vowels(word):\n",
    "    '''display any vowels found in supplied word'''\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return bool(found)   # 添加这行代码 可以通过查看是word中否有元音字母来反馈一个结果\n",
    "search_vowels('mikolo')   # --->True   如果找到元音字母就会返回True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "befa7f5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_vowels('dgfhtr')   # --->False  如果没找到元音字母就会返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2c3b42d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ——return 返回多个值  \n",
    "def search_vowel(word):\n",
    "    '''return a boolean based on any vowels found'''\n",
    "    vowel = set('aeiou')\n",
    "    return vowel.intersection(set(word))\n",
    "search_vowel('shdureoi')  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "02232a90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_vowel('gghytr')    # set{} 这表示的是返回一个空集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cdf82c12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践四：—— 通过注释改进文档\n",
    "def search4vowels(word:str) ->set:     # word是字符串类型  返回值是一个集合\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels.intersection(set(word))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1178f03b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_vowel('villigerr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d980fc9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word: str) -> set\n",
      "    Return a boolean based on any vowels found\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)   # 通过help来帮助查看函数的注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0d1d4eb",
   "metadata": {},
   "source": [
    "#### 模块 （Modules） \n",
    "* 学习文档：<http://c.biancheng.net/view/4633.html>\n",
    "* 定义：模块就是 Python 程序 理解：模块可以比作一盒积木，通过它可以拼出多种主题的玩具，一个函数仅相当于一块积木，而一个模块（.py 文件）中可以包含多个函数，也就是很多积木。  \n",
    "* 用途：能够实现某一特定功能的代码编写在同一个 .py 文件中，并将其作为一个独立的模块，这样既可以方便其它程序或脚本导入并使用，同时还能有效避免函数名和变量名发生冲突。  \n",
    "---\n",
    "* 例子：在某一目录下（桌面也可以）创建一个名为 hello.py 文件，其包含的代码如下：\n",
    "```\n",
    "def say ():\n",
    "    print(\"Hello,World!\")\n",
    "```\n",
    "\n",
    "* 在同一目录下，再创建一个 say.py 文件，其包含的代码如下：\n",
    "```\n",
    "# 通过 import 关键字，将 hello.py 模块引入此文件\n",
    "import hello\n",
    "hello.say()\n",
    "```  \n",
    "\n",
    "* 运行 say.py 文件，其输出结果为：\n",
    "```\n",
    "Hello,World!\n",
    "```\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61de79b4",
   "metadata": {},
   "source": [
    "1. import 模块名 [as 别名]：使用这种语法格式的 import 语句，会导入指定模块中的所有成员（包括变量、函数、类等）。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8183a55c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入sys整个模块\n",
    "import sys\n",
    "# 使用sys模块名作为前缀来访问模块中的成员\n",
    "print(sys.argv[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bd092e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入sys整个模块，并指定别名为s\n",
    "import sys as s\n",
    "# 使用s模块别名作为前缀来访问模块中的成员\n",
    "print(s.argv[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00945800",
   "metadata": {},
   "source": [
    "2. from 模块名 import 成员名 [as 别名]： 使用这种语法格式的 import 语句，只会导入模块中指定的成员，而不是全部成员。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63ed6c60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入sys模块的argv成员\n",
    "from sys import argv\n",
    "# 使用导入成员的语法，直接使用成员名访问\n",
    "print(argv[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97705a63",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入sys模块的argv成员，并为其指定别名v\n",
    "from sys import argv as v\n",
    "# 使用导入成员（并指定别名）的语法，直接使用成员的别名访问\n",
    "print(v[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7f1ea5d",
   "metadata": {},
   "source": [
    "* 模块的导入：[学习文档](http://c.biancheng.net/view/2397.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f02c7079",
   "metadata": {},
   "source": [
    "#### Python编码规范（PEP 8）\n",
    "\n",
    "1. 每个 import 语句只导入一个模块，尽量避免一次导入多个模块  \n",
    "2. 不要在行尾添加分号，也不要用分号将两条命令放在同一行  \n",
    "3. 建议每行不超过 80 个字符，如果超过，建议使用小括号将多行内容隐式的连接起来，而不推荐使用反斜杠 \\ 进行连接  \n",
    "4. 使用必要的空行可以增加代码的可读性，通常在顶级定义（如函数或类的定义）之间空两行，而方法定义之间空一行，另外在用于分隔某些功能的位置也可以空一行  \n",
    "5. 通常情况下，在运算符两侧、函数参数之间以及逗号两侧，都建议使用空格进行分隔  \n",
    "\n",
    "**相关的[学习文档](https://www.cnblogs.com/bymo/p/9567140.html)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c23b54fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
